﻿using Abp.Dependency;
using Castle.MicroKernel.Registration;
using Prism.Ioc;
using System;

namespace Abp.Prism.Ioc
{
    public class AbpContainerExtension : IContainerExtension<IIocManager>
    {
        public AbpContainerExtension(IIocManager iocManager)
        {
            this.Instance = iocManager;
        }
        public IIocManager Instance { get; }

        public bool SupportsModules { get { return true; } }

        public void FinalizeExtension()
        {
        }

        public void Register(Type from, Type to)
        {
            Instance.IocContainer.Register(Component.For(from).ImplementedBy(to).LifestyleTransient());
        }

        public void Register(Type from, Type to, string name)
        {
            Instance.IocContainer.Register(Component.For(from).ImplementedBy(to).Named(name).LifestyleTransient());
        }

        public void RegisterInstance(Type type, object instance)
        {
            Instance.IocContainer.Register(Component.For(type).Instance(instance));
        }

        public void RegisterSingleton(Type from, Type to)
        {
            Instance.IocContainer.Register(Component.For(from).ImplementedBy(to).LifestyleSingleton());
        }

        public object Resolve(Type type)
        {
            return Instance.Resolve(type);
        }

        public object Resolve(Type type, string name)
        {
            return Instance.IocContainer.Resolve(name, type);
        }

        public object ResolveViewModelForView(object view, Type viewModelType)
        {
            return Instance.IocContainer.Resolve(viewModelType);
        }
    }
}
